using Godot;
using GoGameClient.script.cmd;
using GoGameClient.script.global;
using GoGameClient.script.network.webSocket;

namespace GoGameClient.script.node.letter;

public partial class LetterGui : Control
{
    private TextureButton _switchButton;
    private TextureRect _timeBackground;
    private TextureRect _dayBackground;
    private string _mapName = "长安城";

    private Label _mapText;

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        _mapText = GetNode<Label>("MapText");
        _dayBackground = GetNode<TextureRect>("DayBackground");
        _dayBackground = GetNode<TextureRect>("DayBackground");
        _timeBackground = GetNode<TextureRect>("TimeBackground");
        _switchButton = GetNode<TextureButton>("SwitchButton");
        SocketControl.Instance().OfListen(new ListenBroadcastConfig<TimeBroadcastMessage>
        {
            Title = "时间广播监听",
            Cmd = (int)Router.Broadcast,
            CmdMethod = (int)BroadcastRouter.Time,
            Result = TimeUpdate
        });
        SocketControl.Instance().Request(new RequestConfig<TimeBroadcastMessage>
        {
            Title = "当前时间",
            Cmd = (int)Router.Common,
            CmdMethod = (int)CommonRouter.CurrentTime,
            Result = TimeUpdate
        });
    }

    public void SetMapName(string name)
    {
        _mapName = name;
    }

    public void SetPosition(Vector2I vector)
    {
        _mapText.Text = $"{_mapName} [{vector.X},{vector.Y}]";
    }

    private void TimeUpdate(TimeBroadcastMessage message)
    {
        _timeBackground.Texture = GD.Load<Texture2D>($"res://resources/Ui/time/{message.Time}.png");
        _dayBackground.Texture = GD.Load<Texture2D>(message.IsNight
            ? "res://resources/Ui/common/daytime-icon.png"
            : "res://resources/Ui/common/night-icon.png");
    }
}